#include <iostream>

using namespace std;

template < class T >
class Stack
{
private:
	struct Link	{
		T *data;
		Link *next;
		Link( T *dat, Link *nxt ) : data( dat ), next( nxt ) {}
	} *head;

public:
	Stack() : head( NULL ) {}
	~Stack()
	{
		while( head != NULL )
			delete pop();
	}

	void push( T *data )
	{
		head = new Link( data, head );
	}

	T *peek() const
	{
		return head != NULL ? head->data : NULL;
	}

	T *pop()
	{
		if( head == NULL ) return NULL;
		T *result = head->data;
		Link *oldHead = head;
		head = head->next;
		delete oldHead;
		return result;
	}
};

int main( int argc, char *argv[] )
{
	int a[20];

	Stack< int > is;
	for( int i = 0; i < 10; i++ ) {
		a[i] = i;
		is.push( a + i );
	}

	for( int i = 0; i < 10; i++ )
		cout << *is.pop() << " ";

	cout << endl;
}

/// thinking in c++ P399
